<template>
  <ul
    class="list"
    @touchmove.prevent="touchMove"
  >
    <li v-for="item in letters" :key="item" :ref="item" @click="handleLetterClick">
      {{item}}
    </li>
  </ul>
</template>
<script>
export default {
  name: 'CityAlphabet',
  props: {
    cities: Object
  },
  computed: {
    letters () {
      const letters = []
      for (let i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  },
  data () {
    return {
      startY: 0,
      timer: null
    }
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  methods: {
    handleLetterClick (e) {
      this.$emit('change', e.target.innerText)
    },
    touchMove (e) {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        const touchY = e.touches[0].clientY
        const index = Math.floor((touchY - this.startY - 83) / 20)
        this.$emit('change', this.letters[index])
      }, 16)
    }
  }
}
</script>
<style lang="stylus" scoped>
@import '~styles/varibles'
.list
  position absolute
  right 0
  top 1.66rem
  bottom 0
  display flex
  flex-direction column
  justify-content center
  width .4rem
  text-align center
  line-height .4rem
  color $bgColor
</style>
